﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

using SharpSoda.Message;

namespace SharpSoda.Common
{
    public class NewtonMessageCodec : IMessageCodec
    {
        private JsonSerializerSettings m_settings = null;

        public NewtonMessageCodec()
        {
            m_settings = new JsonSerializerSettings();
            m_settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        }

        public string ToString(object obj)
        {
            return JsonConvert.SerializeObject(obj, m_settings);
        }
        public string ToString<T>(T obj) where T : class
        {
            return JsonConvert.SerializeObject(obj as T, m_settings);
        }
        public T FromString<T>(string str) where T : class
        {
            if (str == null || str.Length <= 0) return null;
            else return JsonConvert.DeserializeObject<T>(str, m_settings);
        }
    }
}
